/**
 * CreateTime   : 2020年8月29日
 * Des          : 资源加载释放管理类
 */

import { SpriteFrame, assetManager, AssetManager, AudioClip, Sprite, isValid, Asset, JsonAsset, director } from "cc";
import { AssetMgr } from "./AssetMgr";
import { EnResfUrl } from "./const/EnResUrl";
import { Handler } from "./event/Handler";

export class ResourceMgr extends AssetMgr {
    private static res: ResourceMgr

    private texture = new Map<string, SpriteFrame>();
    private textureMap = new Map<Node, Map<string, number>>();

    static get instance() {
        if (!this.res) {
            this.res = new ResourceMgr();
        }

        return this.res;
    }
    static destory() {
        if (this.res) {
            this.res = null;
        }
    }

    Init() { }

    // 加载音频
    public LoadAudioClip(name: string, completeCallback: Handler) {
        let url = EnResfUrl.audio + "/" + name;
        if (this.resLoading.has(url))
            return;
        this.resLoading.set(url, true);
        let self = this;
        assetManager.loadBundle(EnResfUrl.audio, function (err: Error, bundle: AssetManager.Bundle) {
            bundle.load<AudioClip>(name, AudioClip, function (error: Error, resource: any) {
                self.resLoading.delete(url);
                if (resource) {
                    resource.addRef();
                }
                if (completeCallback) {
                    completeCallback.runWith(resource);
                }

            })
        })
    }
    // 释放音频
    public RealseAudioClip(name: string) {
        this.RealseCache(name, EnResfUrl.audio);
    }
    /**
     * 加载UI预制体Bundle
     * @param name 资源名
     * @param completeCallback 回调
     */
    public LoadUIPrefab(name: string, completeCallback: Handler) {
        this.LoadPrefabBundle(EnResfUrl.uiPrefab, name, completeCallback);
    }

    PlayTalkingData(url: any, asset: any) {
        let ret = 0;
        if (!asset || !asset.nativeUrl) {
            ret = 1;
        }
    }

    /**
     * 预加载UI预制体
     * @param name 资源名
     * @param completeCallback 回调
     */
    public PreLoadUIPrefab(name: string, completeCallback: Handler) {
        assetManager.loadBundle(EnResfUrl.uiPrefab, function (err: Error, bundle: AssetManager.Bundle) {
            bundle.preload(name, Asset, function (error: Error, asset: any) {
                if (completeCallback)
                    completeCallback.run()
            })
        });
    }


    // 释放已加载资源
    RealseCache(url: string, bundletype: EnResfUrl) {
        let bundle = assetManager.getBundle(bundletype);
        if (null != bundle) {
            bundle.release(url);
        }
    }

    // 加载场景
    public LoadScene(name: string, completeCallback: Handler) {
        director.loadScene(name, function () {
            if (completeCallback) {
                completeCallback.run()
            }
        });
    }

    //加载骨骼动画
    public LoadSpinePrefab(name: string, completeCallback: Handler) {
        this.LoadPrefabBundle(EnResfUrl.spine, name, completeCallback);
    }

}